import java.util.Stack;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: lenovo
 * Date: 2023 -04 -12
 * Time: 17:02
 */
public class RPN {
    public static int pnr(String[] s){
        Stack<Integer> stack=new Stack<>();
        for (String x:s) {
            if(!judge(x)){
                stack.push(Integer.parseInt(x));
        }else{
                //注意最先弹出的是操作符的右边的数据，所以应该是：b+a,b-a,b*a,b/a;
                int a =stack.pop();
                int b=stack.pop();
                switch(x){
                    case"+":
                       stack.push(b+a);
                        break;
                    case"-":
                        stack.push(b-a);
                        break;
                    case"*":
                        stack.push(b*a);
                        break;
                    case"/":
                        stack.push(b/a);
                        break;
                }
            }
        }
        return stack.pop();
    }
    public static Boolean judge(String x){
        if(x.equals("+")||x.equals("-")||x.equals("*")||x.equals("/")){
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        //这里要传入一个逆波兰表达式
        String[] s=new String[]{"1","2","3","*","+","1","-","5","5","/","+"};
        int ret=pnr(s);
        System.out.println(ret);
    }
}
